﻿using System.Collections.Generic;
using JStore.Core.Commands;
using JStore.Core.Entities;
using JStore.Data.Daos;

namespace JStore.Tasks
{
    /// <summary>
    ///
    /// </summary>
    public class UserTypeJdRateConfigTask
    {
        private readonly UserTypeJdRateConfigDao _userTypeJdRateCofnigDao = new UserTypeJdRateConfigDao();

        public void DeleteById(int id)
        {
            _userTypeJdRateCofnigDao.DeleteById(id);
        }

        public List<UserTypeJdRateConfig> GetListByUserTypeId(int userTypeId)
        {
            return _userTypeJdRateCofnigDao.GetListByUserTypeId(userTypeId);
        }

        public bool Save(SaveUserTypeJdRateConfigCmd cmd, out string errMsg)
        {
            errMsg = string.Empty;

            if (cmd.AreaRate <= 0 || cmd.AreaRate >= 1)
            {
                errMsg = "区代提成必须为0-1之间的小数";
                return false;
            }

            if (cmd.AreaRate <= 0 || cmd.AreaRate >= 1)
            {
                errMsg = "提成必须为0-1之间的小数";
                return false;
            }

            if (cmd.LayerIndex <= 0)
            {
                errMsg = "提点层数必须是正整数";
                return false;
            }

            var info = new UserTypeJdRateConfig
                           {
                               AreaRate = cmd.AreaRate,
                               LayerIndex = cmd.LayerIndex,
                               Rate = cmd.Rate,
                               UserTypeId = cmd.UserTypeId,
                           };
            _userTypeJdRateCofnigDao.Add(info);
            return true;
        }
    }
}

